<?php
session_start();
ob_start();
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="public/CSS/works.css" type="text/css"/>
        <title>pham thanh anh chau</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <div id="bg">
            <div class="forum">
                <?php include './html/header.php'; ?>

                <h1 align="center" style="margin-top: 82px;">WORKS</h1>
                <div class="content">
                    <div class="view_show"></div>
                    <div class="tabbar">
                        <?php include './html/tabbar.php'; ?>
                        <?php
                        if (!isset($_SESSION['id'])) {
                            require_once './login.php';
                        } else {
                            require_once './login.php';
                        }
                        ?>
                        <?php
                        if (isset($_GET["logout"]) && $_GET["logout"] == true) {
                            unset($_SESSION["id"]);
                            header("location:index.php");
                        }
                        ?>
                    </div>
                    <?php
                    require_once './model/db.php';
                    require_once './model/works.php';
                    require_once './model/nav.php';
//        require_once './model/keywords.php';
//        $keys = new Keywords();
//        $key = $keys->maxlength();
                    $works = new Works();
                    $paginations = new Pagination();
                    ?>
                    <div class="cont_area">
                        <?php
                        if (!isset($_GET["page"]))
                            $page = 1;
                        else
                            $page = $_GET["page"];
                        $list = $works->select_index($page);
                        $total = $works->countWords();
                        echo $paginations->Display($page, $total, null, "index.php");
                        ?><br>                   
                        <table border="0px" align="center">
                            <tr class="works">
                                <?php if (isset($_SESSION['id']) == NULL) { ?>
                                    <td><b>STT</b></td>
                                    <td><b>name</b></td>
                                    <td><b>description</b></td>
                                    <td><b>Images</b></td>
                                    <td><b>posted</b></td>
                                <?php } else { ?>
                                    <td><b>STT</b></td>
                                    <td><b>name</b></td>
                                    <td><b>description</b></td>
                                    <td><b>Images</b></td>
                                    <td><b>posted</b></td>
                                    <?php
                                    if (!isset($_SESSION['id']) == 0) :
                                        ?>
                                        <td><b>upload</b></td>
                                    <?php else : ?>
                                        <td><b>upload</b></td>
                                    <?php
                                    endif;
                                }
                                ?>
                            </tr>
                            <?php if (isset($_SESSION['id']) == NULL) { ?>
                                <?php foreach ($list as $item): ?>
                                    <tr>
                                        <td class="echo"><?php echo$item['id']; ?></td>
                                        <td class="bgtable1"><a href="<?php echo$item['url']; ?>" style="text-decoration: none">
                                                <?php echo$item['name']; ?></a></td>                                            
                                        <td class="bgtable2"><?php echo substr($item['description'], 0, 100); ?></td>
                                        <td class="echo"><img src='<?php echo $item['image'] ?>' width="40px" height="40px"></td>
                                        <td class="echo"><?php $mydate = getdate($item['posted']); ?> 
                                            <?php echo "$mydate[mday] $mydate[month], $mydate[year]"; ?></td>
                                    <?php endforeach; ?>
                                <?php }else { ?>
                                    <?php if (isset($_SESSION['id'])) { ?>
                                        <?php foreach ($list as $item): ?>
                                        <tr>
                                            <td class="echo"><?php echo$item['id']; ?></td>
                                            <td class="bgtable1"><a href="<?php echo$item['url']; ?>" style="text-decoration: none">
                                                    <?php echo$item['name']; ?></a></td>                                            
                                            <td class="bgtable2"><?php echo substr($item['description'], 0, 100); ?></td>
                                            <td class="echo"><img src='<?php echo $item['image'] ?>' width="40px" height="40px"></td>
                                            <td class="echo"><?php $mydate = getdate($item['posted']); ?> 
                                                <?php echo "$mydate[mday] $mydate[month], $mydate[year]"; ?></td>
                                            <?php
                                            if (isset($_SESSION['id']) && $_SESSION['id'] == 0) :
                                                ?>
                                                <td class="echo"><a href="upload.php?id=<?php echo $item['id']; ?>">Edit</a><br>
<!--                                                    <a href="view.php?id=<?php //echo $item['id']; ?>">View</a><br>-->
                                                    <a href="#" class="ajax_view" id_keyword="<?php echo $item['id']?>" id_des="<?php echo $item['description']?>">View</a><br>
                                                    <a href="#">Delete</a>
                                                </td>
                                            <?php else : ?>
                                                <td class="echo">
<!--                                                    <a href="view.php?id=<?php // echo $item['id']; ?>">View</a><br>-->
                                                    <a href="#" class="ajax_view" id_keyword="<?php echo $item['id']?>" id_des="<?php echo $item['description']?>">View</a><br>
                                                </td>
                                            <?php
                                            endif;
                                            ?>
                                        </tr>
                                    <?php endforeach; ?>
                                    <?php
                                }
                                else
                                    header("location:index.php");
                            }
                            ?>
                        </table>
                        <br>
                        <?php echo $paginations->Display($page, $total, null, "index.php"); ?>
                        <br>
                    </div>
                    <div style="clear: both"></div>
                </div>
                <?php include './html/footer.php'; ?>
            </div>
        </div>
        <div>&nbsp</div>
        <script type="text/javascript">
                $( document ).ready(function() {
                    $('.ajax_view').click(function(){
                        var id_keyword  = $(this).attr('id_keyword');
                        var id_des = $(this).attr('id_des');
                        url='ajax.php?id_keyword='+id_keyword+id_des;
                        $.ajax({
                            url: url
                          }).done(function(data) {
                            $('.view_show').html(data);
                          });
                    })
                });
        </script>
    </body>
</html>
<?php ob_end_flush(); ?>
